//
//  Html5ViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/10.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "Html5ViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
@interface Html5ViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    float length;
    NSMutableData *webData;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    
    __weak IBOutlet UIView *noNetWorkView;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@end

@implementation Html5ViewController
- (void)viewWillDisappear:(BOOL)animated {
    NSArray *arr = self.navigationController.viewControllers;
    if (!arr) {
        _webView.delegate = nil;
        [_webView removeFromSuperview],_webView = nil;
        _progressProxy.webViewProxyDelegate = nil;
        _progressProxy.progressDelegate = nil;
        [_progressView removeFromSuperview],_progressView = nil;
    }
}
- (void)viewDidAppear:(BOOL)animated {
    [self requestPart];
    
    if (_doComeFromeHomeView) {
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.frame = CGRectMake(ScreenWidth-8-18, 20+(44-18)/2, 18, 18);
        [shareBtn setImage:[UIImage imageNamed:@"refresh_btn.png"] forState:UIControlStateNormal];
        [shareBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -20, -10, -10)];
        [shareBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
        [self.navBar addSubview:shareBtn];
    }else {
        //是商品详情页 显示分享按钮 否则不显示
        if (_doDetailType) {
            [self initShareBtn];
        }
        
    }
}
//刷新数据
- (void)refresh {
    [_webView reload];
}
- (void)backAction:(UIButton *)btn {
    //判断是否可以HTML返回
    if (![_webView canGoBack]) {
        if (!self.navigationController.viewControllers) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }else {
        [_webView goBack];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BackColor;
    [self initNav:_titleStr doBack:YES];
    
    [self loadMessPart];
    
}
- (void)initShareBtn {
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(ScreenWidth-8-25, 20+(44-18)/2, 25, 18);
    [shareBtn setImage:[UIImage imageNamed:@"share_logo.png"] forState:UIControlStateNormal];
    [shareBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -20, -10, -10)];
    [shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:shareBtn];
}
- (void)loadMessPart {
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.f;
    CGRect barFrame = CGRectMake(0, 64, ScreenWidth, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    [self.view addSubview:_progressView];
}
- (void)requestPart {
    
#pragma mark - html页面地址拼接
    
    NSString *tempUrl = [NSString stringWithFormat:@"%@plat=ios",_requestUrl];
    NSString *reqUrl = _requestUrl;
    if (_doDetailType) {
        reqUrl = [NSString stringWithFormat:@"%@&username=%@&sign=%@",tempUrl,[AppData share].username,[Utils md5SignStr]];
    }
    NSURL *url = [NSURL URLWithString:reqUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_progressView setProgress:1 animated:NO];
    [self cleanMemory];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"网络不给力"];
        noNetWorkView.hidden = NO;
        _webView.hidden = YES;
        [_progressView setProgress:1 animated:YES];
    }
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    BOOL doAlloc = [JsWebviewNativeManager theRequestUrl:[request.URL absoluteString] viewController:self success:^(JsWithWB type, NSString *count) {
        if (type == SendNeedLogin) {
            [_webView goBack];
        }
    }];
    return doAlloc;
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
//    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
#pragma mark - 清除内存
- (void)cleanMemory {
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark - 分享
- (void)shareAction {
//    [[ShareView share] show];
    NSString *url = [NSString stringWithFormat:@"%@&sharedUser=%@",_requestUrl,[AppData share].username];
    [[ShareView share] showMess:[NSString stringWithFormat:@"%@%@",_titleStr,url] withImgUrl:_shareImgUrl url:url];
}


#pragma mark - NJKWebViewProgressDelegate

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
